//
//  CLDataBaseHandle.m
//  I-want-go
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLDataBaseHandle.h"

@implementation CLDataBaseHandle

+ (instancetype)shareDataBasaHandle {
    
    static CLDataBaseHandle *handle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[CLDataBaseHandle alloc] init];
    });
    [handle openDB];
    return handle;
}

- (void)openDB {
    
    // 1 沙盒路径
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
    NSString *dbPath = [sandBoxPath stringByAppendingPathComponent:@"Product.sqlite"];
    int result = sqlite3_open([dbPath UTF8String], &dbpoint);
    
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
    
    NSLog(@"%@", dbPath);
}

// 创建数据库
- (void)createTable {
    
//    NSString *sqlStr = @"create table if not exists CLguidang (productId text, title text, image text)";
     NSString *sqlStr = @"create table if not exists CLguidang (productId text, title text, image text)";
    
    // 执行语句
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    } else {
        NSLog(@"创建失败");
    }
//
}

// 查询有无重复
- (SelectCLguidang)isHaveInTable:(CLguidang *)guidang {
    // 除了openDB 都先写sql语句
//    NSString *sqlStr = [NSString stringWithFormat:@"select * from guidang where productId = '%@' and flag = '1'", guidang.productId];
    NSString *sqlStr = [NSString stringWithFormat:@"select * from CLguidang where title = '%@'", guidang.title];
    // 跟随指针
    sqlite3_stmt *stmt = nil;
    // 执行sql语句
    int result = sqlite3_prepare_v2(dbpoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSLog(@"%d", result);
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            // 在
            return Intable;
        } else {
            sqlite3_finalize(stmt);
            // 不在
            return NotInTable;
        }
    } else {
        sqlite3_finalize(stmt);
        NSLog(@"查询失败");
        
        return SelectError;
    }

    }

//// 收藏
- (void)saveMovieInDB:(CLguidang *)guidang {
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into CLguidang (productId, title, image) values ('%@', '%@', '%@')", guidang.productId, guidang.title, guidang.image];
    
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    }
    else {
        NSLog(@"添加失败");
    }
}

- (void)deleteGuiInDB:(CLguidang *)guidang {
    
    NSString *sqlStr = [NSString stringWithFormat:@"delete from CLguidang where productId = '%@'", guidang.productId];
    
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }
    else {
        NSLog(@"删除失败");
    }
}

- (NSMutableArray *)selectArrMovie {
    NSMutableArray *guidangArr = [NSMutableArray array];
    
    NSString *sqlStr = @"select * from CLguidang";
    
    sqlite3_stmt *stmt = nil;
    
    // 执行查找
    int result = sqlite3_prepare_v2(dbpoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            CLguidang *guidang = [[CLguidang alloc] init];
            guidang.title = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)];
            guidang.productId = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 0)];
            guidang.image = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 2)];
            [guidangArr addObject:guidang];
            [guidang release];
            
        }
    }
    sqlite3_finalize(stmt);
    return guidangArr;
}








@end
